package Views;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.Color;
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import Controller.DBportal;

import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class HomeView extends JPanel implements ActionListener{
	
	private static final int ID_WIDTH = 20;
	private static final int NAME_WIDTH = 150;
	private static final int STYLE_WIDTH = 70;
	private static final int DATE_WIDTH = 100;
	private static final String OPEN = "Open";
	private static final String DELETE = "Delete";
	
	private static final long serialVersionUID = 1L;
	
	private MainWindow parent;
	private DBportal portal;
	
	private Vector<Vector<String>> tableData;
	private Vector<String> columnNames;
	private JTable recipeTable;
	/**
	 * Create the panel.
	 */
	public HomeView(MainWindow parent, DBportal portal) {
		this.parent = parent;
		this.portal = portal;
		tableData = portal.getAllRecipeTableEntries();
		columnNames = new Vector<String>();
		columnNames.add("ID");
		columnNames.add("Name");
		columnNames.add("Style");
		columnNames.add("Brewed On");
		
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0, 0));
		
		createButtons();
		createTable();
	}
	
	private void createButtons() {
		JPanel pnlButton = new JPanel();
		pnlButton.setBackground(SystemColor.menu);
		add(pnlButton, BorderLayout.SOUTH);
		pnlButton.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnOpen = new JButton(OPEN);
		btnOpen.addActionListener(this);
		pnlButton.add(btnOpen);
		
		JButton btnDelete = new JButton(DELETE);
		btnDelete.addActionListener(this);
		pnlButton.add(btnDelete);
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int option = JOptionPane.showConfirmDialog(null,"Continue?", "Are you sure?", JOptionPane.YES_NO_OPTION);
				if(option == JOptionPane.YES_OPTION) {
					portal.closeConnections();
					System.exit(0);
				}
			}
		});
		pnlButton.add(btnExit);
		
	}
	
	/*
	 * Creates the Table & populates it with all the data.
	 */
	private void createTable() {
					//create a table that is not editable
		recipeTable = new JTable(tableData, columnNames) {
			private static final long serialVersionUID = 1L;
				public boolean isCellEditable(int row, int column) {                
		                return false;               
		        };
		    };
		recipeTable.getColumnModel().getColumn(0).setPreferredWidth(ID_WIDTH);
		recipeTable.getColumnModel().getColumn(1).setPreferredWidth(NAME_WIDTH);
		recipeTable.getColumnModel().getColumn(2).setPreferredWidth(STYLE_WIDTH);
		recipeTable.getColumnModel().getColumn(3).setPreferredWidth(DATE_WIDTH);
		
		recipeTable.setAutoCreateRowSorter(true);
		recipeTable.setRowSelectionAllowed(true);
		recipeTable.setSelectionMode(0);
		recipeTable.setBackground(Color.WHITE);

		JScrollPane tablePane = new JScrollPane(recipeTable);
		this.add(tablePane, BorderLayout.CENTER);

	}
	
	/*
	 * Opens a Recipe & notifies user that the recipe has been loaded.
	 * MainWindow will update it's active recipes.
	 */
	private void openRecipe() {
		int selectedRow = recipeTable.getSelectedRow();
		if(selectedRow > -1) {//-1 means no row is selected
			//send idRecipe from the selected row to MainWindow so the correct recipe can be pulled from
			//the database
			parent.setRecipe((String)recipeTable.getValueAt(selectedRow, 0));
			JOptionPane.showMessageDialog(null, "Load Successful");
		}
	}
	

    /*
     * When open is clicked, use getrow, then getvalueat(row,0) to get the id
     */
	/*
	DefaultTableModel model = (DefaultTableModel) recipeTable.getModel();
	model.addRow(new Object[]{"Column 1", "Column 2", "Column 3","Column 4"});
	model.removeRow(3);*/

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//System.out.println(e.getActionCommand());
		//switch(e.getActionCommand())
		switch(e.getActionCommand()) {
		case OPEN: openRecipe(); break;
		case DELETE: //TODO break;
		}
		
	}
}
